package cn.tedu.rediscluster;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class RedisClusterApplication {

	@Autowired
	private RedisConnectionFactory f;

	// redis自动配置中会创建两个redisTemplate实例,此处根据名字注入stringRedisTemplate
	@Autowired
	@Qualifier("stringRedisTemplate")
	private RedisTemplate redisTemplate;

	public static void main(String[] args) {
		SpringApplication.run(RedisClusterApplication.class, args);
	}

	@PostConstruct
	public void test() {
		RedisClusterConnection c = f.getClusterConnection();
		for (int i = 0; i < 100; i++) {
			String k = "key" + i;
			String v = "value" + i;
			c.set(k.getBytes(),v.getBytes());
		}

		byte[] bytes = c.get("key99".getBytes());
		String s = new String(bytes);
		System.out.println(s);
		System.out.println("--------------");

		ValueOperations o = redisTemplate.opsForValue();
		o.set("cookie", "牛逼");
		Object cookie = o.get("cookie");
		System.out.println(cookie);
	}

}
